﻿using SerialPortHelperLib;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XChipTool
{
  public  class ParameterConfig
    {

        //function selection
        int nr3d_enable = 0;
        int hdr_mode = 0;  //0:disable      2:2expo hdr      3:3expo hdr
        int isp_scale_sel = 0;
        int afc_enable = 0;
        //勾选Other的时候写成1   因为当前是页面勾选的时候才会去使用方法  这个不需要处理
        int mjpeg_enable = 0;
        //勾选 IQswitch 的时候写成1  因为当前是页面勾选的时候才会去使用方法  这个不需要处理
        int iQswitch_config = 0;

        uint hsize_in = 1920;
        uint vsize_in = 1080;
        //uint vts_in           =1125;
        uint max_expo_line = 2240;   //sensor max exposure, the value from sensor

        uint sensor_i2c_id = 0x60;
        uint sensor_i2c_mode = 1;       //0:addr8data8 1:addr16data8 2:addr16data16
        uint type_gain = 1;       //0:Type0,1:Patch,2:SOI,3:GC2093,4:GC2053,5:GC2083,6:MIS2031,7:SC200AI,
                                            //8:SC500AI,9:SC830AI,a:SC1336,b:SC2336,c:OS02K10,d:IMX307,e:IMX298
        uint type_exp = 1;       //0:Type0,1:Patch,2:SOI,3:GC2093,4:GC2053,5:GC2083,6:MIS2031,7:SC200AI,
                                           //8:SC500AI,9:SC830AI,a:SC1336,b:SC2336,c:OS02K10,d:IMX307,e:IMX298
                                           //if type_gain =Type0, need config sensor_gain_addr
        //uint sensor_gain_addr0 = 0x3e06;  //addr_high
        //uint sensor_gain_addr1 = 0x3e07;
        //uint sensor_gain_addr2 = 0x0000;
        //uint sensor_gain_addr3 = 0x0000;
        uint[] sensor_gain_addr ={0x3e06,0x3e07,0x0000 ,0x0000};
        //uint sensor_gain_mask0 = 0xffff;  //mask_high
        //uint sensor_gain_mask1 = 0xffff;
        //uint sensor_gain_mask2 = 0x0000;
        //uint sensor_gain_mask3 = 0x0000;
        uint[] sensor_gain_mask = { 0xffff, 0xffff, 0x0000, 0x0000 };
        //if type_exp =Type0, need config sensor_gain_addr
        //uint sensor_exp_addr0 = 0x3e00;  //addr_high
        //uint sensor_exp_addr1 = 0x3e01;
        //uint sensor_exp_addr2 = 0x3e02;
        //uint sensor_exp_addr3 = 0x0000;
        uint[] sensor_exp_addr = { 0x3e00, 0x3e01, 0x3e02, 0x0000 };
        //uint sensor_exp_mask0 = 0xffff;  //mask_high
        //uint sensor_exp_mask1 = 0xffff;
        //uint sensor_exp_mask2 = 0xffff;
        //uint sensor_exp_mask3 = 0x0000;
        uint[] sensor_exp_mask = { 0xffff, 0xffff, 0xffff, 0x0000 };

        int long2short_delay = 40;   // max short exposure lines
        int short2vs_delay = 10;   // max veryshort exposure lines
        uint gain_shift = 4;
        uint exp_shift = 4;

        //if IQswitch_config =1, need config sensor_blc_addr
        uint snr_blc_mode = 0;     //0:off,  1:no page,   2:have page
        uint snr_blc_page_addr = 0xfe;
        uint snr_blc_page_value = 0x02;
        uint[] snr_blc_addr = { 0x3908, 0, 0, 0 };
        uint snr_blc_channel_num = 1;

        uint banding_mode = 0;        //0:disable,  1:60Hz,  2:50Hz
        uint flicker60HzUnit = 0xea0;    //60HzUnit = snr_pclk/(HTS*120)
        uint flicker50HzUnit = 0x1190;   //60HzUnit = snr_pclk/(HTS*100)

        //USB still parameter for Manual_3A
        uint Cofficient_self = 0x40;
        uint[] sensor_hts_addr = { 0x0342, 0x0343 };
        uint[] sensor_vts_addr = { 0x0340, 0x0341 };
        long SENSOR_PCLK = 90000000;//90M HTS*VTS*fps

        public int Nr3d_enable
        {
            get
            {
                return nr3d_enable;
            }

            set
            {
                nr3d_enable = value;
            }
        }

        public int Hdr_mode
        {
            get
            {
                return hdr_mode;
            }

            set
            {
                hdr_mode = value;
            }
        }

        public uint Hsize_in
        {
            get
            {
                return hsize_in;
            }

            set
            {
                hsize_in = value;
            }
        }

        public uint Vsize_in
        {
            get
            {
                return vsize_in;
            }

            set
            {
                vsize_in = value;
            }
        }

        public uint Max_expo_line
        {
            get
            {
                return max_expo_line;
            }

            set
            {
                max_expo_line = value;
            }
        }

        public uint Sensor_i2c_id
        {
            get
            {
                return sensor_i2c_id;
            }

            set
            {
                sensor_i2c_id = value;
            }
        }

        public uint Sensor_i2c_mode
        {
            get
            {
                return sensor_i2c_mode;
            }

            set
            {
                sensor_i2c_mode = value;
            }
        }

        public uint Type_gain
        {
            get
            {
                return type_gain;
            }

            set
            {
                type_gain = value;
            }
        }

        public uint Type_exp
        {
            get
            {
                return type_exp;
            }

            set
            {
                type_exp = value;
            }
        }
        public uint[] Sensor_gain_addr
        {
            get
            {
                return sensor_gain_addr;
            }

            set
            {
                sensor_gain_addr = value;
            }
        }
        //public uint Sensor_gain_addr0
        //{
        //    get
        //    {
        //        return sensor_gain_addr0;
        //    }

        //    set
        //    {
        //        sensor_gain_addr0 = value;
        //    }
        //}

        //public uint Sensor_gain_addr1
        //{
        //    get
        //    {
        //        return sensor_gain_addr1;
        //    }

        //    set
        //    {
        //        sensor_gain_addr1 = value;
        //    }
        //}

        //public uint Sensor_gain_addr2
        //{
        //    get
        //    {
        //        return sensor_gain_addr2;
        //    }

        //    set
        //    {
        //        sensor_gain_addr2 = value;
        //    }
        //}

        //public uint Sensor_gain_addr3
        //{
        //    get
        //    {
        //        return sensor_gain_addr3;
        //    }

        //    set
        //    {
        //        sensor_gain_addr3 = value;
        //    }
        //}
        public uint[] Sensor_gain_mask
        {
            get
            {
                return sensor_gain_mask;
            }

            set
            {
                sensor_gain_mask = value;
            }
        }
        //public uint Sensor_gain_mask0
        //{
        //    get
        //    {
        //        return sensor_gain_mask0;
        //    }

        //    set
        //    {
        //        sensor_gain_mask0 = value;
        //    }
        //}

        //public uint Sensor_gain_mask1
        //{
        //    get
        //    {
        //        return sensor_gain_mask1;
        //    }

        //    set
        //    {
        //        sensor_gain_mask1 = value;
        //    }
        //}

        //public uint Sensor_gain_mask2
        //{
        //    get
        //    {
        //        return sensor_gain_mask2;
        //    }

        //    set
        //    {
        //        sensor_gain_mask2 = value;
        //    }
        //}

        //public uint Sensor_gain_mask3
        //{
        //    get
        //    {
        //        return sensor_gain_mask3;
        //    }

        //    set
        //    {
        //        sensor_gain_mask3 = value;
        //    }
        //}
        public uint[] Sensor_exp_addr
        {
            get
            {
                return sensor_exp_addr;
            }

            set
            {
                sensor_exp_addr = value;
            }
        }
        //public uint Sensor_exp_addr0
        //{
        //    get
        //    {
        //        return sensor_exp_addr0;
        //    }

        //    set
        //    {
        //        sensor_exp_addr0 = value;
        //    }
        //}

        //public uint Sensor_exp_addr1
        //{
        //    get
        //    {
        //        return sensor_exp_addr1;
        //    }

        //    set
        //    {
        //        sensor_exp_addr1 = value;
        //    }
        //}

        //public uint Sensor_exp_addr2
        //{
        //    get
        //    {
        //        return sensor_exp_addr2;
        //    }

        //    set
        //    {
        //        sensor_exp_addr2 = value;
        //    }
        //}

        //public uint Sensor_exp_addr3
        //{
        //    get
        //    {
        //        return sensor_exp_addr3;
        //    }

        //    set
        //    {
        //        sensor_exp_addr3 = value;
        //    }
        //}
        public uint[] Sensor_exp_mask
        {
            get
            {
                return sensor_exp_mask;
            }

            set
            {
                sensor_exp_mask = value;
            }
        }
        //public uint Sensor_exp_mask0
        //{
        //    get
        //    {
        //        return sensor_exp_mask0;
        //    }

        //    set
        //    {
        //        sensor_exp_mask0 = value;
        //    }
        //}

        //public uint Sensor_exp_mask1
        //{
        //    get
        //    {
        //        return sensor_exp_mask1;
        //    }

        //    set
        //    {
        //        sensor_exp_mask1 = value;
        //    }
        //}

        //public uint Sensor_exp_mask2
        //{
        //    get
        //    {
        //        return sensor_exp_mask2;
        //    }

        //    set
        //    {
        //        sensor_exp_mask2 = value;
        //    }
        //}

        //public uint Sensor_exp_mask3
        //{
        //    get
        //    {
        //        return sensor_exp_mask3;
        //    }

        //    set
        //    {
        //        sensor_exp_mask3 = value;
        //    }
        //}

        public int Long2short_delay
        {
            get
            {
                return long2short_delay;
            }

            set
            {
                long2short_delay = value;
            }
        }

        public int Short2vs_delay
        {
            get
            {
                return short2vs_delay;
            }

            set
            {
                short2vs_delay = value;
            }
        }

        public int Isp_scale_sel
        {
            get
            {
                return isp_scale_sel;
            }

            set
            {
                isp_scale_sel = value;
            }
        }

        public int Afc_enable
        {
            get
            {
                return afc_enable;
            }

            set
            {
                afc_enable = value;
            }
        }

        public int Mjpeg_enable
        {
            get
            {
                return mjpeg_enable;
            }

            set
            {
                mjpeg_enable = value;
            }
        }

        public int IQswitch_config
        {
            get
            {
                return iQswitch_config;
            }

            set
            {
                iQswitch_config = value;
            }
        }

        public uint Exp_shift
        {
            get
            {
                return exp_shift;
            }

            set
            {
                exp_shift = value;
            }
        }

        public uint Snr_blc_mode
        {
            get
            {
                return snr_blc_mode;
            }

            set
            {
                snr_blc_mode = value;
            }
        }

        public uint Snr_blc_page_addr
        {
            get
            {
                return snr_blc_page_addr;
            }

            set
            {
                snr_blc_page_addr = value;
            }
        }

        public uint Snr_blc_page_value
        {
            get
            {
                return snr_blc_page_value;
            }

            set
            {
                snr_blc_page_value = value;
            }
        }

        public uint[] Snr_blc_addr
        {
            get
            {
                return snr_blc_addr;
            }

            set
            {
                snr_blc_addr = value;
            }
        }

        public uint Snr_blc_channel_num
        {
            get
            {
                return snr_blc_channel_num;
            }

            set
            {
                snr_blc_channel_num = value;
            }
        }

        public uint Banding_mode
        {
            get
            {
                return banding_mode;
            }

            set
            {
                banding_mode = value;
            }
        }

        public uint Flicker60HzUnit
        {
            get
            {
                return flicker60HzUnit;
            }

            set
            {
                flicker60HzUnit = value;
            }
        }

        public uint Flicker50HzUnit
        {
            get
            {
                return flicker50HzUnit;
            }

            set
            {
                flicker50HzUnit = value;
            }
        }

        public uint Cofficient_self1
        {
            get
            {
                return Cofficient_self;
            }

            set
            {
                Cofficient_self = value;
            }
        }

        public uint[] Sensor_hts_addr
        {
            get
            {
                return sensor_hts_addr;
            }

            set
            {
                sensor_hts_addr = value;
            }
        }

        public uint[] Sensor_vts_addr
        {
            get
            {
                return sensor_vts_addr;
            }

            set
            {
                sensor_vts_addr = value;
            }
        }

        public long SENSOR_PCLK1
        {
            get
            {
                return SENSOR_PCLK;
            }

            set
            {
                SENSOR_PCLK = value;
            }
        }

        public uint Gain_shift
        {
            get
            {
                return gain_shift;
            }

            set
            {
                gain_shift = value;
            }
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="sPortTemp">串口</param>
        /// <param name="isUsb">是否是USB设备</param>
        public void Initialization(SerialPort sPortTemp,bool isUsb = true)
        {
            
                nr3d_enable = (SerialSend.ReadRegister(0x80300002, sPortTemp) & 0x8) == 0x8 ? 1 : 0;
                isp_scale_sel = (SerialSend.ReadRegister(0x80300001, sPortTemp) & 0x10) == 0x10 ? 1 : 0;
                afc_enable = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;

                if (((SerialSend.ReadRegister(0x80300013, sPortTemp) & 0x80) == 0x80))
                {
                    hdr_mode = 2;
                }
                if (((SerialSend.ReadRegister(0x80300013, sPortTemp) & 0x40) == 0x40))
                {
                    hdr_mode = 3;
                }
                if ((SerialSend.ReadRegister(0x80300013, sPortTemp) == 0x00))
                {
                    hdr_mode = 0;
                }
               
                //mjpeg_enable = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;
                //IQswitch_config = (SerialSend.ReadRegister(0x20000037, sPortTemp) & 0xFF) == 0xFF ? 1 : 0;
                //hsize_in  8030 1f04 1f05
                hsize_in = Convert.ToUInt32($"{SerialSend.ReadRegister(0x80301f04, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x80301f05, sPortTemp).ToString("X2")}",16);
                //vsize_in 8030 1f06 1f07
                vsize_in = Convert.ToUInt32($"{SerialSend.ReadRegister(0x80301f06, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x80301f07, sPortTemp).ToString("X2")}", 16);
                // max_expo_line 0x200000c7 0x200000c6  0x200000c5  0x200000c4
                max_expo_line = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000c7, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c6, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c5, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x200000c4, sPortTemp).ToString("X2")}", 16);
                //sensor_i2c_id 0x200000fe
                sensor_i2c_id = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000fe, sPortTemp).ToString("X2")}", 16);
                //sensor_i2c_mode 0x200000ff
                sensor_i2c_mode = Convert.ToUInt32($"{SerialSend.ReadRegister(0x200000ff, sPortTemp).ToString("X2")}", 16);
                //type_gain  0x20000100
                type_gain = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000100, sPortTemp).ToString("X2")}", 16);
                //type_exp  0x20000101
                type_exp = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000101, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_addr0  0x20000128 0x20000129
                sensor_gain_addr[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000128, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000129, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_addr1  0x2000012a 0x2000012b
                sensor_gain_addr[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012a, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012b, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_addr2 0x2000012c  0x2000012d
                sensor_gain_addr[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012c, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012d, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_addr3  0x2000012e 0x2000012f
                sensor_gain_addr[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000012e, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000012f, sPortTemp).ToString("X2")}", 16);

                //sensor_gain_mask0 0x20000130 0x20000131
                sensor_gain_mask[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000130, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000131, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_mask1 0x20000132 0x20000133
                sensor_gain_mask[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000132, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000133, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_mask2 0x20000134 0x20000135
                sensor_gain_mask[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000134, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000135, sPortTemp).ToString("X2")}", 16);
                //sensor_gain_mask3 0x20000136 0x20000137
                sensor_gain_mask[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000136, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000137, sPortTemp).ToString("X2")}", 16);

                //sensor_exp_addr0 0x20000108 0x20000109
                sensor_exp_addr[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000108, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000109, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_addr1 0x2000010a 0x2000010b
                sensor_exp_addr[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010a, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010b, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_addr2 0x2000010c 0x2000010d
                sensor_exp_addr[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010c, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010d, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_addr3 0x2000010e 0x2000010f
                sensor_exp_addr[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x2000010e, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x2000010f, sPortTemp).ToString("X2")}", 16);

                //sensor_exp_mask0  0x20000110  0x20000111
                sensor_exp_mask[0] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000110, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000111, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_mask1 0x20000112 0x20000113
                sensor_exp_mask[1] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000112, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000113, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_mask2  0x20000114 0x20000115
                sensor_exp_mask[2] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000114, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000115, sPortTemp).ToString("X2")}", 16);
                //sensor_exp_mask3  0x20000116  0x20000117
                sensor_exp_mask[3] = Convert.ToUInt32($"{SerialSend.ReadRegister(0x20000116, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000117, sPortTemp).ToString("X2")}", 16);

                //long2short_delay   0x20000257 0x20000256 0x20000255 0x20000254
                long2short_delay = Convert.ToInt32($"{SerialSend.ReadRegister(0x20000257, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000256, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000255, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000254, sPortTemp).ToString("X2")}", 16);
                //short2vs_delay  0x20000260  0x20000261 0x20000262 0x20000263
                short2vs_delay = Convert.ToInt32($"{SerialSend.ReadRegister(0x20000260, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000261, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000262, sPortTemp).ToString("X2")}{SerialSend.ReadRegister(0x20000263, sPortTemp).ToString("X2")}", 16);
            
           

        }

    }
}
